<?php

	$epcms = new epcms_base();

	echo "<h2>Add new article</h2>";
	
	if (isset($_REQUEST['node'])) { $current_node = $_REQUEST['node']; }
	else { $current_node = 0; }
	
	$breadcrumb = $epcms->getBreadcrumb($current_node);	
	
	// Dibuja la ruta de nodos dond eestamos actualmente
	echo "<div class=\"epcms_current_path\">";	
	
		echo "<a href=\"?app=epcms\">Root</a> / ";
		
		if (count($breadcrumb) > 0)
		{
			foreach ($breadcrumb as $node_id => $node_title)
			{			
				if ($node_id) { echo "<a href=\"?app=epcms&node=". $node_id ."\">". htmlentities($node_title) ."</a> / "; }
			}	
		}
	
	echo "</div>";
	
	$publish = "";
	
	if (!Group::getAccess($global_user->group_id, $this->app_name, "EPCMS_ARTICLES_PUBLISHED")) {
		$publish = "disabled=\"disabled\"";
	}
?>

<form method="post" id="frm-addarticle" action="index.php">

<input type="hidden" name="node" value="<?php echo $_REQUEST['node']; ?>">
<input type="hidden" name="app" value="epcms">
<input type="hidden" name="acc" value="addarticle">

	<!-- Article Sumary -->
	<fieldset class="fieldset">	
		<legend><a onClick="$('#field_sumary').slideToggle(); return false;" title="Article sumary" href="#">Sumary</a></legend>
		
		<div id="field_sumary">
			<label for="title">Title: </label><br>
			<input style="width:99%;" type="text" id="title" name="title" value=""><br>			
			
			<label for="description_short">Short description: </label><br>
			<textarea cols="46" rows="5" style="width:99%;" id="description_short" name="description_short"></textarea><br>			

			<label for="description">Description: </label><br>
			<textarea cols="46" rows="5" style="width:99%;" id="descripcion" name="description"></textarea>
		</div>
		
	</fieldset><br>
    
    <fieldset class="fieldset">
    	<legend><a onClick="$('#field_image').slideToggle(); return false;" title="Image" href="#">Image</a></legend>
        
        <div id="field_image">
        	
            <label for="type_image">Type image:</label>
            
            <select name="type_image" id="type_image" onchange="validImage();">
            <option value="0" selected="selected">-- SELECT --</option>
            
            <?php 
			
				$types = $epcms->getTypeImagenByType(1); // is article!
				
				foreach ($types as $type) {
					echo "<option value=\"".$type['id']."\">".$type['name']."</option>";
				}
			
			?>
            
            </select><br />
            
        	<input name="select_image" id="select_image" class="button imagenmanager" type="button" value="SELECT IMAGE" disabled="disabled" /><br /><br />
            <div class="nota_image"></div>
            <div class="mostrar_ruta"></div>
            
        </div>
    </fieldset><br>
	
	<!-- Article Settings -->
	<fieldset class="fieldset">	
		<legend><a onClick="$('#field_settings').slideToggle(); return false;" title="Article settings" href="#">Settings</a></legend>
		
		<div id="field_settings">
		
			<label for="published">Published: </label>
			<input type="checkbox" id="published" name="published" value="1" <?php echo $publish ?>><br>			
			
			<label for="shedule_publish">Shedule publish: </label>
			<input readonly style="width:140px;" type="text" id="shedule_publish" name="shedule_publish" value="" <?php echo $publish ?>> 
            
            <?php if ($publish == "") { ?>
            <a onClick="$('#shedule_publish').val('');return false;" title="Don't shedule" href="#noshedule">X</a>
            <?php } ?>
            <br>
			<label for="created_on">Created on: </label>
			<input readonly style="width:140px;" type="text" id="created_on" name="created_on" value="<?php echo date('d-m-Y h:m:s', time()); ?>"><br>			
			
			<label for="last_update">Last update: </label>
			<input readonly style="width:140px;" type="text" id="last_update" name="last_update" value=""><br>						
			
			<!--<label for="parent_article">Parent article: </label>
			<input readonly style="width:140px;" type="text" id="titulo" name="titulo" value=""> <small><a class="parent_select" href="<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/templates/select_parent.php">Select</a> | <a title="Remove parent" href="index.php">Remove</a></small><br>-->
			
		</div>
		
	</fieldset><br>	
	
	<!-- Article SEO -->
	<fieldset class="fieldset">	
				
		<legend><a onClick="$('#field_seo').slideToggle(); return false;" title="Search engine optimization settings" href="#">SEO</a></legend>
		
		<div id="field_seo" style="display:none;">
			<label for="seo_title">Title: </label><br>
			<input style="width:99%;" type="text" id="seo_title" name="seo_title" value=""><br>			
			
			<label for="seo_url">URL: </label><br>
			<input style="width:99%;" type="text" id="seo_url" name="seo_url" value=""><br>			
			
			<label for="seo_description">Description: </label><br>
			<textarea cols="46" rows="5" style="width:99%;" id="seo_description" name="seo_description"></textarea><br>			
			
			<label for="seo_keyword">Keywords (coma-separated): </label><br>
			<input style="width:99%;" type="text" id="seo_keyword" name="seo_keyword" value=""><br>						
			
		</div>
		
	</fieldset><br>	
	
	<!-- Article HTML content -->
	<fieldset class="fieldset">	
		<legend>Article content</legend><br>
		<?php
			// Gestor de imagenes
			//include "app/epcms/templates/imgmanager.php";
		?>
		<br>
		<div style="float:left;width:100%;">
			<textarea class="article_text" cols="60" rows="5" id="article_text" name="article_text"></textarea><br>			
		</div>
		<!--<div style="border:1px solid green;float:right;width:200px;">
			img manager
		</div><br>-->
		
	</fieldset>
	<br>
	
	<input class="button" style="margin-right:20px;" type="submit" name="addArticle" value="Save changes">
	<input onClick="document.location.href='?app=epcms&node=<?php echo $current_node; ?>';" class="button" type="button" name="btnCancel" value="Cancel"><br>
	
</form>

<script type="text/javascript">

//<![CDATA[
	// Replace the <textarea id="editor"> with an CKEditor
	// instance, using default configurations.
	
	<?php
		// Cargamos la hoja de estilo predefinida para ckEditor
		$ck_css_files 	= config::getValue("epcms", "EPCMS_EDITOR_CSS_URL");
		$ck_height 		= config::getValue("epcms", "EPCMS_EDITOR_HEIGHT");
	?>

	CKEDITOR.replace( 'article_text',
		{
			extraPlugins : 'MediaEmbed',
			extraPlugins : 'stylesheetparser',
			height: <?php echo $ck_height; ?>,
			contentsCss : ['<?php echo $ck_css_files; ?>','<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/css/css_ck.css','<?php echo _CONFIG_INSTALL_DIR_; ?>/css/4d-systems-style.css'],
			entities:false,
			stylesSet : []
		});
		
		<?php if (Group::getAccess($global_user->group_id, $this->app_name, "EPCMS_ARTICLES_PUBLISHED")) { ?>
		// Time picker pluggin: http://trentrichardson.com/examples/timepicker/
		$('#shedule_publish').datetimepicker({
			showSecond: true,
			dateFormat: 'yy-mm-dd',
			timeFormat: 'hh:mm:ss',
			hourGrid: 4,
			minuteGrid: 10			
		});		
		
		<?php } ?>
//]]>

	$(document).ready(function() {
		
		// SEO Auto write
		$('#title').keyup(function() {
		  $('#seo_title').val($('#title').val());
		  $('#seo_url').val(compose_seo_url($('#title').val()));
		});	
		
		$('#description').keyup(function() {		  
		  $('#seodescription').val(compose_seo_url($('#description').val()));
		});			
				
		// Foco y obligatorios
		$('#title').focus();
		
		
		$('#frm-addarticle').preSubmit({
			errClass: 'errorField',
			fields: '#title, #seo_title, #seo_url'
		});		  

		// Select parent
		//$(".parent_select").colorbox({inline:true, width:"50%"});
		
		$('#type_image').bind('change', function(){ 
           //alert($(this).val()); 
		   
		   
			$(".imagenmanager").colorbox({
				href:"<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/templates/imgmanager.php?type_image=" + $(this).val(),
				iframe:true,
				width:960,
				height:500,
				transition:"true",
				/*maxWidth:900,
				maxHeight:( $("body").height() - 20 ),*/
				opacity:0.50,
				onComplete: function() {
					//alert($('#type_image').bind('change').val());
				},
				
				onClosed: function() {
					//$(".mostrar_ruta").html('');
				}
				
	
			});
			
			
			$('.nota_image').empty();
			if ($(this).val() !=0) {
			 
			  $.ajax({
				  type: 'POST',
				  url : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/ajax/js_show_image.php',
				  data: 'accion=ver_nota&id='+$(this).val()+'',
				  success : function (data) {
					  //alert(data); 																				
					$('.nota_image').html(data);
					  
				  },
				  error : function (data) { 								
					  alert('Error de llamada AJAX: '+data);
				  }					
			  });
			}
		   
        }); 
		
		//$('#type_image').bind('change').val()
		
		
	});
	
	function validImage(){
		//alert($('#type_image').val());
				if ($('#type_image').val() == 0)
					$('#select_image').attr('disabled', 'disabled');
				else
					$('#select_image').removeAttr('disabled');
	}	

	function meterImagen(data) { 
	 // $('.mostrar_ruta').html(data);
	  
	  
	  $.ajax({
		  type: 'POST',
		  url : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/ajax/js_show_image.php',
		  data: 'accion=ver_imagen&id='+data+'',
		  success : function (data) { 																				
			$('.mostrar_ruta').html(data);
			  
		  },
		  error : function (data) { 								
			  alert('Error de llamada AJAX: '+data);
		  }					
	  });			
	  
	  
	} 
	
	function DeleteImage() {
		if(confirm('Remove this picture?')) {
			$("#type_image option[value=0]").attr("selected",true);
			$('.mostrar_ruta').empty();
			$('.nota_image').empty();
		}
	}
	

	
	
</script>
